package com.wn.ziroom.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageInfo;
import com.wn.utils.CookieUtils;

import com.wn.utils.JwtUtils;
import com.wn.ziroom.entity.Category;
import com.wn.ziroom.entity.Production;
import com.wn.ziroom.entity.User;
import com.wn.ziroom.entity.vo.ProductionVo;
import com.wn.ziroom.service.CategoryService;
import com.wn.ziroom.service.ProductionService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wangzhijia
 * @since 2023-02-23 02:27:54
 */
@Controller
@RequestMapping("/ziroom_portal/production")
public class ProductionController {

    private static final Logger logger = LoggerFactory.getLogger(ProductionController.class);

    private ProductionService productionService;

    private CategoryService categoryService;

    @Autowired
    public void setCategoryService(CategoryService categoryService) {
        this.categoryService = categoryService;
    }

    @Autowired
    public void setProductionService(ProductionService productionService) {
        this.productionService = productionService;
    }

    @RequestMapping("/index")
    public String index(){
        return "ziroomFileIndex";
    }

    @GetMapping("/productionList")
    public ModelAndView productionList(@RequestParam(value = "now",defaultValue = "1") Integer now,
                                       @RequestParam(value = "size",defaultValue = "2") Integer size,
                                       String productionName,
                                       String categoryId,
                                       HttpServletRequest httpServletRequest){
        ModelAndView mav = new ModelAndView();

        PageInfo<ProductionVo> productionPage = productionService.listBootByPage(now, size, productionName,categoryId);
        List<ProductionVo> productionList = productionPage.getList();

        for (int i = 0; i < productionList.size(); i++) {
            for (int i1 = 0; i1 < productionList.get(i).getImageList().size(); i1++) {
                System.out.println("图片地址:"+productionList.get(i).getImageList().get(i1).getImageUrl());
            }
        }

        //-- 获取用户信息
        String token = CookieUtils.getCookie("token", httpServletRequest);
        System.out.println("token----------------->"+token);
        if (Objects.isNull(token)){
            mav.addObject("userInfo",null);
        }else {
            User userInfo = JwtUtils.verifyToken(token, User.class);
            if (Objects.isNull(userInfo)){
                mav.addObject("userInfo",null);
            }else {
                mav.addObject("userInfo",userInfo);
            }
        }

        List<Category> categoryList = categoryService.queryCategoryList();
        //-- 断言,判断下得到的数据不为空
        assert categoryList != null;
        logger.debug((categoryList != null) + "");

        mav.setViewName("shoppingList");
        mav.addObject("productionList",productionList);
        mav.addObject("categoryList",categoryList);
        //- 即使返回的是逻辑视图名称,SpringMVC的DispatcherServlet 1071行 会把数据封装成ModelAndView对象
        return mav;
    }

    @RequestMapping("/productionDetail")
    public ModelAndView productionDetail(@RequestParam("productionId") Integer productionId,HttpServletRequest httpServletRequest){
        System.out.println("-------productionId-------"+productionId);
        ProductionVo productionVo = productionService.findProductionById(productionId);

        ModelAndView modelAndView = new ModelAndView();

        //-- 获取用户信息
        String token = CookieUtils.getCookie("token", httpServletRequest);
        if (Objects.isNull(token)){
            modelAndView.addObject("userInfo",null);
        }else {
            User userInfo = JwtUtils.verifyToken(token, User.class);
            if (Objects.isNull(userInfo)){
                modelAndView.addObject("userInfo",null);
            }else {
                modelAndView.addObject("userInfo",userInfo);
            }
        }

        modelAndView.addObject("productionVo",productionVo);

        modelAndView.setViewName("productionDetail");

        return modelAndView;
    }
}

